package au.com.iglootech.winerymap.ui.view.winerymanage;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import au.com.iglootech.core.IGTIView;
import au.com.iglootech.core.IGTIViewPresenter;
import au.com.iglootech.winerymap.entity.WineryInfo;
import au.com.iglootech.winerymap.ui.presenter.winerymanage.WineryFieldFactory;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;

/**
 * 
 * @author Nicholas Zhu
 * 
 */
@Configurable(preConstruction = true)
@Component
@Scope("request")
public class CreateWineryTab extends CustomComponent implements IGTIView {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7109861402039039817L;

	private static final String[] FIELDS = new String[] { "companyName", "contactName", "contactNumber", "email", "address1", "address2", "address3",
			"description" };

	@Autowired
	private IPresenter presenter;
	@Autowired
	private WineryFieldFactory factory;

	private final VerticalLayout mainLayout = new VerticalLayout();
	private WineryInfo wineryInfo;
	private BeanItem<WineryInfo> personItem;

	/**
	 * constructor.
	 */
	public CreateWineryTab() {
		setCaption("Add Winery");
		wineryInfo = new WineryInfo();
		initContent();
	}

	/**
	 * 
	 */
	@SuppressWarnings("serial")
	private void initContent() {
		Panel panel = new Panel();
		final Form wineryForm = new Form();

		personItem = new BeanItem<WineryInfo>(wineryInfo);
		wineryForm.setCaption("Company details");
		wineryForm.setWriteThrough(false); // we want explicit 'apply'
		wineryForm.setInvalidCommitted(false); // no invalid values in
												// datamodel

		// FieldFactory for customizing the fields and adding validators
		wineryForm.setFormFieldFactory(factory);
		wineryForm.setItemDataSource(personItem); // bind to POJO via BeanItem

		// Determines which properties are shown, and in which order:
		wineryForm.setVisibleItemProperties(Arrays.asList(FIELDS));
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setSpacing(true);

		Button apply = new Button("Apply", new Button.ClickListener() {
			@Override
			public void buttonClick(final ClickEvent event) {
				try {
					wineryForm.commit();
					// create winery
					presenter.createWinery(wineryInfo);
					getWindow().showNotification("You company has been saved.");
					wineryInfo = new WineryInfo();
					wineryForm.discard();
					personItem = new BeanItem<WineryInfo>(wineryInfo);
					wineryForm.setItemDataSource(personItem);
					wineryForm.setVisibleItemProperties(Arrays.asList(FIELDS));
				} catch (Exception e) {
					getWindow().showNotification(e.getMessage(), Notification.TYPE_WARNING_MESSAGE);
				}
			}
		});
		buttons.addComponent(apply);
		wineryForm.getFooter().addComponent(buttons);
		wineryForm.getFooter().setMargin(false, false, true, true);
		panel.addComponent(wineryForm);
		mainLayout.addComponent(panel);
		this.setCompositionRoot(mainLayout);
	}

	/**
	 * 
	 * @author Nicholas
	 * 
	 */
	public interface IPresenter extends IGTIViewPresenter {
		/**
		 * 
		 * @param info
		 *            entity
		 * @return persistent entity
		 */
		WineryInfo createWinery(WineryInfo info);
	}
}
